package com.ds.infrastructure.hyperspace.container.flow.handler.zone;

import com.ds.infrastructure.hyperspace.common.Container;
import com.ds.infrastructure.hyperspace.common.flow.FlowHandlerPipeline;
import com.ds.infrastructure.hyperspace.common.flow.handler.PreFlowHandler;
import com.ds.infrastructure.hyperspace.container.flow.context.HyperspaceDuplexFlowContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

/**
 * usage eg:
 *  -   handler: staticZonePreHandler
 *      handlerParams: [sh]
 * @author  Weishaoying
 */
@Slf4j
public class StaticZonePreFlowHandler implements PreFlowHandler<HyperspaceDuplexFlowContext> {

    private volatile String zone;

    @Override
    public void handleInput(HyperspaceDuplexFlowContext duplexFlowContext, Object data) {
        if (data != duplexFlowContext.getRequestStartState() || StringUtils.isEmpty(zone)) {
            return;
        }
        duplexFlowContext.setZoneId(zone);
    }

    @Override
    public String handlerName() {
        return "staticZonePreHandler";
    }

    @Override
    public void initHandlerParameters(Container container, FlowHandlerPipeline pipeline, String[] handlerParams) {
        if (handlerParams.length > 0 && !StringUtils.isEmpty(handlerParams[0])) {
            zone = handlerParams[0].toLowerCase();
        }
    }

}
